#include "LineSeg.h"

namespace Engine
{
	LineSeg::LineSeg(const Vec3& start, const Vec3& end)
	{
		m_start = start;
		m_end = end;
	}

	void LineSeg::SetPoints(const Vec3& start, const Vec3& end)
	{
		m_start = start;
		m_end = end;
	}

	Vec3 LineSeg::GetPoint(float t)
	{
		return Vec3(m_start + (m_end - m_start) * t);
	}

	float LineSeg::SqrDistToPoint(const Vec3& point)
	{
		Vec3 m = m_end - m_start;
		Vec3 q = point - m_start;
		float t = DotProduct(m, q) / DotProduct(m, m);

		Vec3 result;

		if(t < 0)
		{
			result = (q);

			return result.SquaredLength();
		}

		else if (t > 1)
		{
			result = (point - m_end);

			return result.SquaredLength();
		}

		else
		{
			Vec3 p = m_start + (m * t);

			result = point - p;

			return result.SquaredLength();
		}
	}
}
